#!/bin/bash
set -e

echo "Deploying to ${DEPLOY_HOST}"

ssh -o StrictHostKeyChecking=no ${DEPLOY_USER}@${DEPLOY_HOST} "
    set -e
    echo 'Logging into Harbor registry'
    echo '${HARBOR_PASS}' | docker login -u '${HARBOR_USER}' --password-stdin http://${HARBOR_REGISTRY}

    echo 'Pulling new image'
    docker pull ${DOCKER_IMAGE}

    echo 'Extracting public files from image'
    docker rm -f temp-extract || true
    docker create --name temp-extract ${DOCKER_IMAGE}
    docker cp temp-extract:/var/www/html/public ${REMOTE_DIR}/public
    docker rm temp-extract

    echo 'Restarting services'
    cd ${REMOTE_DIR}
    docker-compose down || true
    docker-compose up -d

    echo 'Deployment completed'
    echo 'Access URL: http://${DEPLOY_HOST}'
"